<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="role">
            <el-row style="height: 60px;">
              <el-button type="primary" size="small" icon="el-icon-plus" @click="showDialog = true">新增角色</el-button>
            </el-row>
            <el-table
              v-loading="loading"
              :data="list"
              style="width: 100%"
              border=""
            >
              <el-table-column
                type="index"
                label="序号"
                width="120"
                align="center"
              />
              <el-table-column
                prop="name"
                label="角色名称"
                width="240"
                align="center"
              />
              <el-table-column
                prop="description"
                label="描述"
                align="center"
              />
              <el-table-column
                prop="address"
                label="操作"
                align="center"
              >
                <template v-slot="{row}">
                  <el-button type="success" size="small" @click="handleAssignPermission(row.id)">分配权限</el-button>
                  <el-button type="primary" size="small" @click="editRole(row.id)">编辑</el-button>
                  <el-button type="danger" size="small" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" align="middle" style="height: 60px;" justify="center">
              <el-pagination
                layout="prev, pager, next"
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="company">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="140px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" disabled style="width:400px" type="textarea" :rows="3" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <add-role ref="roleFormRef" :show-dialog.sync="showDialog" @addRole="getRoleList" />
      <assign-permission ref="AssignPerm" :show-permission-dialog.sync="showPermissionDialog" />
    </div>
  </div>
</template>

<script>
import AddRole from './components/add-role.vue'
import { getRoleList, getCompanyInfo, deleteRole } from '@/api/setting'
import AssignPermission from './components/assign-permisson.vue'
import { mapGetters } from 'vuex'
export default {
  components: {
    AddRole,
    AssignPermission
  },
  data() {
    return {
      activeName: 'role',
      list: [],
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
        total: 0 // 记录总数
      },
      loading: false,
      formData: {
        // 公司信息
      },
      showDialog: false,
      showPermissionDialog: false // 权限分配弹框
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  mounted() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList() {
      this.loading = true
      const { rows, total } = await getRoleList(this.page)
      this.loading = false
      this.list = rows
      this.page.total = total
    },
    // 点击删除角色
    async deleteRole(roleId) {
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await deleteRole(roleId)
        this.$message.success('删除角色成功')
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    // 点击编辑角色信息
    async editRole(roleId) {
      await this.$refs.roleFormRef.getRoleDetail(roleId) // 等详情数据回来在显示
      this.showDialog = true
    },
    // 公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },

    // 当前页码发生变化的时候
    handleCurrentChange(page) {
      this.page.page = page
      this.getRoleList()
    },
    // 点击分配权限
    async handleAssignPermission(roleId) {
      await this.$refs.AssignPerm.getRoleDetail(roleId)
      this.showPermissionDialog = true
    }
  }
}
</script>

<style>

</style>
